package cn.innosmart.aq.manager;

import com.tutk.p2p.ConnectionEntity;
import com.tutk.p2p.ResponseHandler;

import org.json.JSONObject;

/**
 * Created by David on 2016/3/4.
 */
public class NetworkManager extends BaseManager {
    private String Tag = "DeviceManager";
    private WlanScanCallBack wlanScanCallBack;
    private ResponseHandler.ResponseCallback mWlanScanCallBack = new ResponseHandler.ResponseCallback() {
        @Override
        public void onResponseCallback(ConnectionEntity connectionEntity, String message) {
            log(Tag, "NetworkManager.WLANScan get response=" + message);
            connectionEntity.flags.remove(connectionEntity.DEVICELIST);
            JSONObject jRoot = null;
            try {
                jRoot = new JSONObject(message);
                int rspCode = jRoot.getInt("code");
                String rspMsg = jRoot.getString("message");
                if (0 == rspCode && "Success".equals(rspMsg)) {
                    JSONObject jsonAp = jRoot.getJSONObject("result");
                    if(wlanScanCallBack!=null){
                        wlanScanCallBack.onWlanScanCallBackCallBack(0,jsonAp.toString());
                    }
//                    JSONArray jsonArray = jRoot.getJSONObject("result").getJSONArray("channels");
                }else{
                    if(wlanScanCallBack!=null){
                        wlanScanCallBack.onWlanScanCallBackCallBack(-1,null);
                    }
                }
            } catch (Exception e) {
                if(wlanScanCallBack!=null){
                    wlanScanCallBack.onWlanScanCallBackCallBack(-1,null);
                }
            }
        }
    };

    private NetworkManager() {
    }

    //NetworkManager.WLANScan

    public static NetworkManager getInstance() {
        return NetworkManagerInstance.instance;
    }

    /**
     * 扫描WiFi
     * params:
     * {
     * }
     * result:
     * {
     * "AP":["AP1", "AP2", "AP3"]
     * }
     *
     * @param connectionEntity
     * @return
     */
    public boolean WLANScan(ConnectionEntity connectionEntity, WlanScanCallBack wlanScanCallBack) {
        this.wlanScanCallBack = wlanScanCallBack;
        long callid = System.currentTimeMillis() / 1000;
        JSONObject jParams = new JSONObject();
        ResponseHandler handler = new ResponseHandler();
        handler.setCallback(mWlanScanCallBack);
        connectionEntity.mReqRspHashMap.put(callid, handler);
        if (!SendCommand(connectionEntity, connectionEntity.getUid(), "NetworkManager.WLANScan", jParams.toString(), callid)) {
            connectionEntity.mReqRspHashMap.remove(callid);
            return false;
        } else {
            return true;
        }
    }
    public interface WlanScanCallBack {
        void onWlanScanCallBackCallBack(int ret,String apJsonString);
    }

    private WlanConfigCallBack wlanConfigCallBack;
    public interface WlanConfigCallBack {
        void onWlanConfigCallBack(int ret,String apJsonString);
    }

    /**
     * NetworkManager.WLANConfig

     配置WiFi

     params:
     {
     "ssid" : "MyAP",
     "password": "123456",
     "encrypt": "WPA2PSK-AES"
     }

     * @param connectionEntity
     * @param wlanConfigCallBack
     * @return
     */
    public boolean WLANConfig(ConnectionEntity connectionEntity,JSONObject jsonParams, WlanConfigCallBack wlanConfigCallBack) {
        this.wlanConfigCallBack = wlanConfigCallBack;
        long callid = System.currentTimeMillis() / 1000;
        ResponseHandler handler = new ResponseHandler();
        handler.setCallback(mWlanConfigCallBack);
        connectionEntity.mReqRspHashMap.put(callid, handler);
        if (!SendCommand(connectionEntity, connectionEntity.getUid(), "NetworkManager.WLANConfig", jsonParams.toString(), callid)) {
            connectionEntity.mReqRspHashMap.remove(callid);
            return false;
        } else {
            return true;
        }
    }

    private ResponseHandler.ResponseCallback mWlanConfigCallBack = new ResponseHandler.ResponseCallback() {
        @Override
        public void onResponseCallback(ConnectionEntity connectionEntity, String message) {
            log(Tag, "NetworkManager.WLANConfig get response=" + message);
            connectionEntity.flags.remove(connectionEntity.DEVICELIST);
            JSONObject jRoot = null;
            try {
                jRoot = new JSONObject(message);
                int rspCode = jRoot.getInt("code");
                String rspMsg = jRoot.getString("message");
                if (0 == rspCode && "Success".equals(rspMsg)) {
                    if(wlanConfigCallBack!=null){
                        wlanConfigCallBack.onWlanConfigCallBack(0, null);
                    }
//                    JSONArray jsonArray = jRoot.getJSONObject("result").getJSONArray("channels");
                }else{
                    if(wlanConfigCallBack!=null){
                        wlanConfigCallBack.onWlanConfigCallBack(-1,null);
                    }
                }
            } catch (Exception e) {
                if(wlanConfigCallBack!=null){
                    wlanConfigCallBack.onWlanConfigCallBack(-1,null);
                }
            }
        }
    };

    private WlanConnectCallBack wlanConnectCallBack;
    public interface WlanConnectCallBack {
        void onWlanConnectCallBack(int ret,String apJsonString);
    }
    public boolean WLANConnect(ConnectionEntity connectionEntity, WlanConnectCallBack wlanConnectCallBack) {
        this.wlanConnectCallBack = wlanConnectCallBack;
        long callid = System.currentTimeMillis() / 1000;
        JSONObject jParams = new JSONObject();
        ResponseHandler handler = new ResponseHandler();
        handler.setCallback(mWlanConnectCallBack);
        connectionEntity.mReqRspHashMap.put(callid, handler);
        if (!SendCommand(connectionEntity, connectionEntity.getUid(), "NetworkManager.WLANConnect", jParams.toString(), callid)) {
            connectionEntity.mReqRspHashMap.remove(callid);
            return false;
        } else {
            return true;
        }
    }

    private ResponseHandler.ResponseCallback mWlanConnectCallBack = new ResponseHandler.ResponseCallback() {
        @Override
        public void onResponseCallback(ConnectionEntity connectionEntity, String message) {
            log(Tag, "NetworkManager.WLANConnect get response=" + message);
            connectionEntity.flags.remove(connectionEntity.DEVICELIST);
            JSONObject jRoot = null;
            try {
                jRoot = new JSONObject(message);
                int rspCode = jRoot.getInt("code");
                String rspMsg = jRoot.getString("message");
                if (0 == rspCode && "Success".equals(rspMsg)) {
                    JSONObject jsonAp = jRoot.getJSONObject("result");
                    if(wlanConnectCallBack!=null){
                        wlanConnectCallBack.onWlanConnectCallBack(0, jsonAp.toString());
                    }
//                    JSONArray jsonArray = jRoot.getJSONObject("result").getJSONArray("channels");
                }else{
                    if(wlanConnectCallBack!=null){
                        wlanConnectCallBack.onWlanConnectCallBack(-1, null);
                    }
                }
            } catch (Exception e) {
                if(wlanConnectCallBack!=null){
                    wlanConnectCallBack.onWlanConnectCallBack(-1, null);
                }
            }
        }
    };

    private WlanDisconnectCallBack wlanDisconnectCallBack;
    public interface WlanDisconnectCallBack {
        void onWlanDisconnectCallBack(int ret,String apJsonString);
    }
    public boolean WLANDisconnect(ConnectionEntity connectionEntity, WlanDisconnectCallBack wlanDisconnectCallBack) {
        this.wlanDisconnectCallBack = wlanDisconnectCallBack;
        long callid = System.currentTimeMillis() / 1000;
        JSONObject jParams = new JSONObject();
        ResponseHandler handler = new ResponseHandler();
        handler.setCallback(mWlanDisconnectCallBack);
        connectionEntity.mReqRspHashMap.put(callid, handler);
        if (!SendCommand(connectionEntity, connectionEntity.getUid(), "NetworkManager.WLANDisconnect", jParams.toString(), callid)) {
            connectionEntity.mReqRspHashMap.remove(callid);
            return false;
        } else {
            return true;
        }
    }

    private ResponseHandler.ResponseCallback mWlanDisconnectCallBack = new ResponseHandler.ResponseCallback() {
        @Override
        public void onResponseCallback(ConnectionEntity connectionEntity, String message) {
            log(Tag, "NetworkManager.WLANDisconnect get response=" + message);
            connectionEntity.flags.remove(connectionEntity.DEVICELIST);
            JSONObject jRoot = null;
            try {
                jRoot = new JSONObject(message);
                int rspCode = jRoot.getInt("code");
                String rspMsg = jRoot.getString("message");
                if (0 == rspCode && "Success".equals(rspMsg)) {
                    JSONObject jsonAp = jRoot.getJSONObject("result");
                    if(wlanDisconnectCallBack!=null){
                        wlanDisconnectCallBack.onWlanDisconnectCallBack(0, jsonAp.toString());
                    }
//                    JSONArray jsonArray = jRoot.getJSONObject("result").getJSONArray("channels");
                }else{
                    if(wlanDisconnectCallBack!=null){
                        wlanDisconnectCallBack.onWlanDisconnectCallBack(-1, null);
                    }
                }
            } catch (Exception e) {
                if(wlanDisconnectCallBack!=null){
                    wlanDisconnectCallBack.onWlanDisconnectCallBack(-1,null);
                }
            }
        }
    };

    private static class NetworkManagerInstance {
        private static final NetworkManager instance = new NetworkManager();
    }
}
